const config = require('../../../config/config.local');
const amazonMws = require('amazon-mws')(config.AWSAccessKeyId, config.AWSSecretKey);
const isProduction = process.env.NODE_ENV === 'production';
const jsonflatten = require('pt-core-common').util.jsonflatten;

const RecommendationCategory = Object.freeze({
  Selection: Symbol('Selection'),
  Fulfillment: Symbol('Fulfillment'),
  ListingQuality: Symbol('ListingQuality'),
  GlobalSelling: Symbol('GlobalSelling'),
  Advertising: Symbol('Advertising'),
});
const FilterOptions = Object.freeze({
  QualitySet_isDefect: Symbol('QualitySet=Defect'),
  QualitySet_isQuarantine: Symbol('QualitySet=Quarantine')
});

const listRecommendations = {};
listRecommendations.generateOption = function(seller_id, mws_auth_token, marketplaceId, categoryQueryList){
  const baseOption = {
    'Version': '2013-04-01',
    'Action': 'ListRecommendations',
    'SellerId': seller_id,
    'MWSAuthToken': mws_auth_token,
    'MarketplaceId': marketplaceId,
    // 'CategoryQueryList.CategoryQuery.1.FilterOptions.FilterOption.1': 'QualitySet=Defect',
    // 'CategoryQueryList.CategoryQuery.1.FilterOptions.FilterOption.2': 'ListingStatus=Active',
    // 'CategoryQueryList.CategoryQuery.1.RecommendationCategory': 'ListingQuality'
  };

  const option = Object.assign(baseOption, jsonflatten.flatten(categoryQueryList, null, baseIndex=1));
  return option;
};

listRecommendations._ = function(options) {
  if (!isProduction) {
    return amazonMws.recommendations.searchFor(options);
  } else {
    console.error("bot not implemented!");
  }
};

listRecommendations.listRecommendations = function(seller_id, mws_auth_token, marketplaceId, categoryQueryList) {
  return this._(this.generateOption(seller_id, mws_auth_token, marketplaceId, categoryQueryList));
};

module.exports = listRecommendations;